<?php

/**
 * backoffice actions.
 *
 * @package    KC-INDICATEURS
 * @subpackage backoffice
 * @author     Antoine Rolland <antoine.rolland@keyconsulting.fr>
 */
class backofficeActions extends sfActions
{
  /**
   * (non-PHPdoc)
   * @see lib/vendor/symfony-1.4.6/lib/action/sfAction::preExecute()
   */
  public function preExecute()
  {
    if(!$this->getUser()->isAuthenticated()
    || !$this->projet = Doctrine::getTable('Projet')->find($this->getUser()->getAttribute('projet_id')))
    {
      $this->getUser()->setAuthenticated(false);
      $this->redirect('transverse/index');
    }
    else{
      $id = $this->getUser()->getAttribute('id');
      $acteur = Doctrine::getTable('Acteur')->findOneByAcronyme($id);
      if ($acteur != null){
        $this->admin = $acteur->isAdmin();
      }
    }
  }

  public function executeIndex(sfWebRequest $request)
  {
    $this->mesures = Doctrine_Core::getTable('Mesure')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new MesureForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new MesureForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($mesure = Doctrine_Core::getTable('Mesure')->find(array($request->getParameter('id'))), sprintf('Object mesure does not exist (%s).', $request->getParameter('id')));
    $this->form = new MesureForm($mesure);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($mesure = Doctrine_Core::getTable('Mesure')->find(array($request->getParameter('id'))), sprintf('Object mesure does not exist (%s).', $request->getParameter('id')));
    $this->form = new MesureForm($mesure);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($mesure = Doctrine_Core::getTable('Mesure')->find(array($request->getParameter('id'))), sprintf('Object mesure does not exist (%s).', $request->getParameter('id')));
    $mesure->delete();

    $this->redirect('backoffice/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $mesure = $form->save();

      $this->redirect('backoffice/edit?id='.$mesure->getId());
    }
  }

  /**
   *
   * Action d'extraction mantis
   *
   * @param sfWebRequest $request
   */
  public function executeMantis(sfWebRequest $request)
  {
    $this->erreur = true;
    $this->message = '';

    $this->redirectUnless($projet = Doctrine::getTable('Projet')->find(
                                    $this->getUser()->getAttribute('projet_id')),
                          '@homepage');
    $projet_mantis_id = $projet->getMantisId();

    //Purge des tables
    Doctrine::getTable('Bug')->findAll()->delete();
    Doctrine::getTable('Critere')->findAll()->delete();
    Doctrine::getTable('Historique')->findAll()->delete();

    try{
      $extracteur = new Extracteur(sfConfig::get('app_mantis_dsn'),
                                   sfConfig::get('app_mantis_user'),
                                   sfConfig::get('app_mantis_pass'));
      $extracteur->extractBugs($projet_mantis_id);
      $this->message = 'Import effectué avec succès';
      $this->erreur = false;
    }
    catch(PDOException $e){
      $message = 'Connexion à mantis en échec : "'.$e->getMessage().'"';
      $this->logMessage($message);
      $this->message = $message;
    }
    catch(sfException $e){
      $message = 'Extraction en échec : "'.$e->getMessage().'"';
      $this->logMessage($message);
      $this->message = $message;
    }

    //Precalcul des deltas dans la table historique
    Doctrine::getTable('Historique')->preCalculDelta();
  }
}
